package com.wulin;

import com.wulin.mapper.UserDataMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Ultimate_System extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
//	JbdcObdcConnection db=new JbdcObdcConnection();
	String resource = "mybatis-config.xml";
	InputStream inputStream = Resources.getResourceAsStream(resource);
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	SqlSession sqlSession = sqlSessionFactory.openSession();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Ultimate_System frame = new Ultimate_System();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Ultimate_System() throws IOException {
		getContentPane().setLayout(null);
//		db = new JbdcObdcConnection();
//        db.getConnection();
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 648, 533);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("农业监测系统");
		label.setFont(new Font("宋体", Font.PLAIN, 24));
		label.setBounds(221, 34, 154, 34);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("用户名");
		label_1.setBounds(127, 119, 80, 15);
		contentPane.add(label_1);
		
		textField = new JTextField();
		textField.setBounds(196, 104, 189, 45);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel label_2 = new JLabel("密码");
		label_2.setBounds(127, 200, 54, 15);
		contentPane.add(label_2);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(196, 185, 189, 45);
		contentPane.add(passwordField);
		
		JButton button = new JButton("登录");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String username = textField.getText(); // ????????
                String password = new String(passwordField.getPassword()); // ???????

				try {
					String userType = checkUserType(username, password);
					if (userType != null) {
						dispose();
						contents_System display = new contents_System(username, userType);
						JOptionPane.showMessageDialog(Ultimate_System.this, "登录成功", "提示", JOptionPane.INFORMATION_MESSAGE);
						display.setVisible(true);
					} else {
						JOptionPane.showMessageDialog(Ultimate_System.this, "用户名或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
					}
				} catch (Exception ex) {
					ex.printStackTrace();
					JOptionPane.showMessageDialog(Ultimate_System.this, "操作失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
				}
//                try {
//                	String userType = checkUserType(username, password);
//                    if (userType != null) {
//                        dispose();
//                        contents_System display = new contents_System(username, userType);
//                        JOptionPane.showMessageDialog(Ultimate_System.this, "??????", "???", JOptionPane.INFORMATION_MESSAGE);
//                        display.setVisible(true);
//                    } else {
//                        JOptionPane.showMessageDialog(Ultimate_System.this, "??????????????", "???", JOptionPane.ERROR_MESSAGE);
//                    }
//                } catch (SQLException ex) {
//                    ex.printStackTrace();
//                } catch (IOException ex) {
//                    throw new RuntimeException(ex);
//                }
            }
		});
		button.setBounds(51, 321, 219, 45);
		contentPane.add(button);
		
		JButton button_1 = new JButton("注册");
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
                zhuce_system popupWindow = null;
                try {
                    popupWindow = new zhuce_system();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                popupWindow.setVisible(true);
			}
		});
		button_1.setBounds(294, 321, 219, 45);
		contentPane.add(button_1);
	} 
	
	private String checkUserType(String username, String password) throws SQLException {
		try {
			java.util.Map<String, Object> params = new java.util.HashMap<>();
			params.put("username", username);
			params.put("password", password);
			// 获取 Mapper 接口
			UserDataMapper userMapper = sqlSession.getMapper(UserDataMapper.class);

			// 调用 MyBatis 方法
			String userType = userMapper.selectUserType(username, password);
			return userType;
		} catch (Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(this, "操作失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
			return null;
		}
//        String sql = "SELECT user_type FROM user_Data WHERE username = ? AND password = ?";
//        PreparedStatement pstmt = db.prepareStatement(sql);
//        pstmt.setString(1, username);
//        pstmt.setString(2, password);
//        ResultSet rs = pstmt.executeQuery();
//        if (rs.next()) {
//            return rs.getString("user_type");
//        }
//        return null;
    }

}
